package edu.codink.xq.center.dao;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import edu.codink.xq.center.dto.UserMenu;
import edu.codink.xq.center.dto.UserMenuItem;

public class MenuConfig
{
	private static final Logger LOGGER = Logger.getLogger(MenuConfig.class);
	private static HashMap<String, UserMenu> menuMap;
	private static MenuConfig MENUCONFIG = new MenuConfig();;

	private MenuConfig()
	{
		menuMap = new HashMap<String, UserMenu>();

		Document doc = getXmlDocument();
		if (doc != null)
		{
			Node root = doc.getDocumentElement();
			if (root != null)
			{
				NodeList nodeList = root.getChildNodes();
				for (int i = 0; i < nodeList.getLength(); i++)
				{
					Node menuNode = nodeList.item(i);
					if (menuNode.getNodeName().toLowerCase().equals("menu"))
					{
						UserMenu userMenu = getUserMenu(menuNode);
						menuMap.put(userMenu.getMenuCode(), userMenu);
					}
				}
			}
		}
		LOGGER.info("完成menu.xml加载");
	}

	public static HashMap<String, UserMenu> getMenuConfigMap()
	{
		return menuMap;
	}

	private UserMenu getUserMenu(Node menuNode)
	{
		UserMenu userMenu = new UserMenu();
		userMenu.setMenuCode(getStringAttribute(menuNode, "code"));
		userMenu.setMenuText(getStringAttribute(menuNode, "text"));
		userMenu.setIconPath(getStringAttribute(menuNode, "icon"));
		NodeList nodeList = menuNode.getChildNodes();

		for (int i = 0; i < nodeList.getLength(); i++)
		{
			Node menuItemNode = nodeList.item(i);
			if (menuItemNode.getNodeName().toLowerCase().equals("submenu"))
			{
				UserMenuItem item = getUserMenuItem(menuItemNode);
				userMenu.addMenuItem(item);
			}
		}
		return userMenu;
	}

	private UserMenuItem getUserMenuItem(Node menuItemNode)
	{
		UserMenuItem item = new UserMenuItem();
		item.setMenuCode(getStringAttribute(menuItemNode, "code"));
		item.setMenuText(getStringAttribute(menuItemNode, "text"));
		item.setMenuUrl(getStringAttribute(menuItemNode, "url"));
		return item;
	}

	private Document getXmlDocument()
	{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder;
		Document doc = null;
		InputStream xmlStream = null;
		try
		{
			docBuilder = factory.newDocumentBuilder();
			xmlStream = MenuConfig.class.getResourceAsStream("/menu.xml");
			doc = docBuilder.parse(xmlStream);
		} catch (ParserConfigurationException e)
		{
			e.printStackTrace();
		} catch (SAXException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		} finally
		{
			if (xmlStream != null)
			{
				try
				{
					xmlStream.close();
				} catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return doc;
	}

	private String getStringAttribute(Node node, String name)
	{
		org.w3c.dom.Node attribute = node.getAttributes().getNamedItem(name);
		if (attribute != null)
			return attribute.getNodeValue();
		else
			return null;
	}

	static MenuConfig getInstance()
	{
		if (MENUCONFIG == null)
		{
			return new MenuConfig();
		} else
		{
			return MENUCONFIG;
		}
	}
}
